#include "bsp_uart.h"

void BSP_UART::BSP_USART_Init(UART_Device_Typedef* dev) {
    GPIO_InitTypeDef  GPIO_InitStructure = {0};
    USART_InitTypeDef USART_InitStructure = {0};
    NVIC_InitTypeDef  NVIC_InitStructure = {0};

    dev->RCC_APB1PeriphClockCmd(dev->RCC_APB1Periph, ENABLE);
    dev->RCC_APB2PeriphClockCmd(dev->RCC_APB2Periph, ENABLE);

    GPIO_InitStructure.GPIO_Pin = dev->tx_pin;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = dev->gpio_tx_pin_mode;
    GPIO_Init(dev->port, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = dev->rx_pin;
    GPIO_InitStructure.GPIO_Mode = dev->gpio_rx_pin_mode;
    GPIO_Init(dev->port, &GPIO_InitStructure);


    USART_InitStructure.USART_BaudRate = dev->BaudRate;
    USART_InitStructure.USART_WordLength = dev->WordLength;
    USART_InitStructure.USART_StopBits = dev->StopBits;
    USART_InitStructure.USART_Parity = dev->Parity;
    USART_InitStructure.USART_HardwareFlowControl = dev->HardwareFlowControl;
    USART_InitStructure.USART_Mode = dev->Mode;

    USART_Init(dev->UARTx, &USART_InitStructure);

    USART_ITConfig(dev->UARTx, dev->USART_IT, ENABLE);


    NVIC_InitStructure.NVIC_IRQChannel = dev->NVIC_IRQChannel;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = dev->NVIC_IRQChannelPreemptionPriority;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = dev->NVIC_IRQChannelSubPriority;
    NVIC_InitStructure.NVIC_IRQChannelCmd = dev->NVIC_IRQChannelCmd;
    NVIC_Init(&NVIC_InitStructure);

    USART_Cmd(dev->UARTx, ENABLE);
    USART_DMACmd(dev->UARTx, dev->USART_DMAReq, ENABLE);
}



void BSP_UART::BSP_DMA_INIT(Device_DMA_Typedef* dev_dma) {
    dev_dma->RCC_AHBPeriphClockCmd(dev_dma->RCC_AHBPeriph, ENABLE);

    DMA_InitTypeDef DMA_InitStructure = {0};

    DMA_DeInit(dev_dma->DMAx_Channely);
    DMA_InitStructure.DMA_PeripheralBaseAddr = dev_dma->DMA_PeripheralBaseAddr;
    DMA_InitStructure.DMA_MemoryBaseAddr = dev_dma->DMA_MemoryBaseAddr;
    DMA_InitStructure.DMA_DIR = dev_dma->DMA_DIR;
    DMA_InitStructure.DMA_BufferSize = dev_dma->DMA_BufferSize;
    DMA_InitStructure.DMA_PeripheralInc = dev_dma->DMA_PeripheralInc;
    DMA_InitStructure.DMA_MemoryInc = dev_dma->DMA_MemoryInc;
    DMA_InitStructure.DMA_PeripheralDataSize = dev_dma->DMA_PeripheralDataSize;
    DMA_InitStructure.DMA_MemoryDataSize = dev_dma->DMA_MemoryDataSize;
    DMA_InitStructure.DMA_Mode = dev_dma->DMA_Mode;
    DMA_InitStructure.DMA_Priority = dev_dma->DMA_Priority;
    DMA_InitStructure.DMA_M2M = dev_dma->DMA_M2M;
    DMA_Init(dev_dma->DMAx_Channely, &DMA_InitStructure);

    DMA_Cmd(dev_dma->DMAx_Channely, ENABLE);
}

BSP_UART::BSP_UART(UART_Device_Typedef* dev, Device_DMA_Typedef* dev_dma_tx, Device_DMA_Typedef* dev_dma_rx)
    :  dev(dev),  dev_dma_tx(dev_dma_tx), dev_dma_rx(dev_dma_rx)
{
    BSP_USART_Init(dev);
    BSP_DMA_INIT(dev_dma_tx);
    BSP_DMA_INIT(dev_dma_rx);
}

BSP_UART::~BSP_UART() {
    USART_DeInit(dev->UARTx);
    DMA_DeInit(dev_dma_tx->DMAx_Channely);
    DMA_DeInit(dev_dma_rx->DMAx_Channely);
}


#ifdef DMA_NOT_NEED
// 非DMA发送
void BSP_UARTR::uart_SendByte(uint8_t Byte)
{
    USART_SendData(USART3, Byte);
    while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}

void uart_SendArray(uint8_t *Array, uint16_t Length)
{
    uint16_t i;
    for (i = 0; i < Length; i ++ )
    {
        uart_SendByte(Array[i]);
    }
}

#endif



